import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12

Rectangle {

    id: root



    // 接口 =====================================================================
    function reanalysis(){
        var params = {
            "FIXED_BASE_END": fixedBase ? Number(lineedit_fixedBase.getText()) : -1,
            "CLIP_START":     Number(lineedit_start.    getText()),
            "SLOPE_THR":      Number(lineedit_upSlope.  getText()),
            "SLOPE_THR_DOWN": Number(lineedit_downSlope.getText()),
            "MIN_CONF":       Number(lineedit_conf.     getText()),
            "RANGE_THR":      Number(lineedit_height.   getText())
        }
        CConfig.reanalysis(changeMode===0 ? curChannel + "_" + curPosi : curChannel, params)
    }
    function initEditor(){
        var res = CConfig.getDefaultParams()
        lineedit_fixedBase.setText( res["FIXED_BASE_END"] )
        lineedit_start    .setText( res["CLIP_START"]     )
        lineedit_upSlope  .setText( res["SLOPE_THR"]      )
        lineedit_downSlope.setText( res["SLOPE_THR_DOWN"] )
        lineedit_conf     .setText( res["MIN_CONF"]       )
        lineedit_height   .setText( res["RANGE_THR"]      )
        fixedBase = res["FIXED_BASE_END"] !== -1
        if(fixedBase){
            radio_open.checked = true;   radio_close.checked = false;
        }else{
            radio_open.checked = false;  radio_close.checked = true;
        }
    }
    function syncEditor(){
        var res = CConfig.getParams(changeMode===0 ? curChannel + "_" + curPosi : curChannel)
        lineedit_fixedBase.setText( res["FIXED_BASE_END"] )
        lineedit_start    .setText( res["CLIP_START"]     )
        lineedit_upSlope  .setText( res["SLOPE_THR"]      )
        lineedit_downSlope.setText( res["SLOPE_THR_DOWN"] )
        lineedit_conf     .setText( res["MIN_CONF"]       )
        lineedit_height   .setText( res["RANGE_THR"]      )
        fixedBase = res["FIXED_BASE_END"] !== -1
        if(fixedBase){
            radio_open.checked = true;   radio_close.checked = false;
        }else{
            radio_open.checked = false;  radio_close.checked = true;
        }
    }
    Connections{
        target: CConfig
        onSelectablePosiListChanged:{
            console.log("重新配置可选的List")
            curChannel = "FAM"
            curPosi = list[0]
            posiPannel.setSelectableList(list)
            syncEditor()
        }
    }


    // 状态控制 ==================================================================
    property string     curChannel: "FAM"
    property string     curPosi: "A1"
    property int        changeMode: 0     // 0: 单选 1: 多选
    property bool       fixedBase: false

    function setCheckChannel(channel){
        radio_CY5.checked = false
        radio_FAM.checked = false
        radio_VIC.checked = false
        radio_ROX.checked = false
        if(channel === "FAM")  { radio_FAM.checked = true; posiPannel.selectedColor = "#99A8FF";}
        if(channel === "VIC")  { radio_VIC.checked = true; posiPannel.selectedColor = "#82F0AF";}
        if(channel === "ROX")  { radio_ROX.checked = true; posiPannel.selectedColor = "#FFC39A";}
        if(channel === "CY5")  { radio_CY5.checked = true; posiPannel.selectedColor = "#FF8387";}
        curChannel = channel

    }

    onCurChannelChanged: {
        syncEditor()
    }onCurPosiChanged: {
        syncEditor()
    }onChangeModeChanged: {
        syncEditor()
    }onVisibleChanged: {
        syncEditor()
    }onFixedBaseChanged: {
        if(fixedBase){
            lineedit_fixedBase.setText(10)
        }else{
            lineedit_fixedBase.setText(-1)
        }
    }


    // 样式

    height: 500
    width: 900


    Column{         // 左侧 ============================================================
        x: 20
        y: 20
        spacing: 10
        height: root.height - 40
        width: root.width / 2 - 30 + 50
        Row{
            height: 40
            width: parent.width
            Item{
                height: parent.height
                width: 120
                Text{
                    text: "单孔位筛选"
                    anchors.verticalCenter: parent.verticalCenter
                }
            }
            CustomSwitchButton{
                anchors.verticalCenter: parent.verticalCenter
                onRelease: {
                    changeMode = checked ? 0 : 1
                }
            }
        }
        PosiPannel{
            id: posiPannel
            visible: changeMode === 0
            width: parent.width
            height: 380
            rowNum: 8
            colNum: 12
            onSelectedChanged: {
                curPosi = name
            }
        }
        Row{
            height: 40
            Item{
                height: parent.height
                width: 50
                Text{
                    text: "通道"
                    anchors.verticalCenter: parent.verticalCenter
                }
            }
            CustomRadio{
                id: radio_FAM
                title: "FAM"
                checked: true
                onReleased: {
                    setCheckChannel(title)
                }
            }CustomRadio{
                id: radio_VIC
                title: "VIC"
                onReleased: {
                    setCheckChannel(title)
                }
            }CustomRadio{
                id: radio_ROX
                title: "ROX"
                onReleased: {
                    setCheckChannel(title)
                }
            }CustomRadio{
                id: radio_CY5
                title: "CY5"
                onReleased: {
                    setCheckChannel(title)
                }
            }
        }
        Item{
            visible: changeMode === 1
            height: 40
            width: 20
            Text {
                text: "*对整个通道进行配置会覆盖单孔位配置的内容"
                color: "red"
                anchors.verticalCenter: parent.verticalCenter
            }
        }
    }
    Column{        // 右侧 ==========================================================
        x: root.width / 2 + 20 + 50
        y: 20
        height: root.height - 40
        width: root.width / 2 - 40 - 50
        Item{
            height: 40; width: parent.width
            Text{
                width: parent.width
                text: "当前筛选:  " + (changeMode===0 ? curChannel+"_"+curPosi : "通道"+curChannel)
                anchors.verticalCenter: parent.verticalCenter
                font.pixelSize: 17
                horizontalAlignment: Text.AlignRight
            }
        }
        RowLayout{
            height: 40
            width: parent.width
            Item{
                height: parent.height
                width: 200
                Text{
                    text: "固定基线期结束点"
                    anchors.verticalCenter: parent.verticalCenter
                }
            }
            CustomHorSpacerItem{}
            CustomRadio{
                id: radio_open
                title: "打开"
                onReleased: {
                    fixedBase = true
                    radio_close.checked = false
                }
            }CustomRadio{
                id: radio_close
                title: "关闭"
                onReleased: {
                    fixedBase = false
                    radio_open.checked = false
                }
            }
        }
        CustomLineEditor{
            id: lineedit_fixedBase
            editWidth: parent.width
            editable: fixedBase
            intflag: true
        }
        Item{ height: 12; width: 20 }       // 占位行 ==============
        Item{
            height: 40
            width: 50
            Text{
                text: "拟合起始循环数"
                anchors.verticalCenter: parent.verticalCenter
            }
        }CustomLineEditor{
            id: lineedit_start
            editWidth: parent.width
            editable: !fixedBase
            intflag: true
        }
        Item{ height: 12; width: 20 }       // 占位行 ==============
        RowLayout{
            height: 40
            width: parent.width
            spacing: 20
            Item{
                height: 40
                width: 50
                Text{
                    text: "上升斜率阈值 (2~5)"
                    anchors.verticalCenter: parent.verticalCenter
                }
            }Item{
                height: 40
                width: 50
                Text{
                    text: "下降斜率阈值 (0.3~1)"
                    anchors.verticalCenter: parent.verticalCenter
                }
            }
        }RowLayout{
            height: 40
            width: parent.width
            spacing: 20
            CustomLineEditor{
                id: lineedit_upSlope
                editWidth: parent.width / 2 - 10
                editable: !fixedBase
                douflag: true
            }CustomLineEditor{
                id: lineedit_downSlope
                editWidth: parent.width / 2 - 10
                editable: !fixedBase
                douflag: true
            }
        }
        Item{ height: 12; width: 20 }       // 占位行 =============
        RowLayout{
            height: 40
            width: parent.width
            spacing: 20
            Item{
                height: 40
                width: 50
                Text{
                    text: "最低置信度 (0.8~1)"
                    anchors.verticalCenter: parent.verticalCenter
                }
            }Item{
                height: 40
                width: 50
                Text{
                    text: "最低扩增高度"
                    anchors.verticalCenter: parent.verticalCenter
                }
            }
        }RowLayout{
            height: 40
            width: parent.width
            spacing: 20
            CustomLineEditor{
                id: lineedit_conf
                editWidth: parent.width / 2 - 10
                editable: !fixedBase
                douflag: true

            }CustomLineEditor{
                id: lineedit_height
                editWidth: parent.width / 2 - 10
                editable: !fixedBase
                intflag: true
            }
        }
        Item{ height: 30; width: 20 }       // 占位行 =============
        RowLayout{
            height: 40
            spacing: 20
            width: parent.width
            CustomHorSpacerItem{}
            CustomPushButton{
                title: "恢复默认参数"
                width: 140
                button_border: "#CCCCCC"
                onReleased: {
                    root.initEditor()
                }
            }CustomPushButton{
                title: "重新分析"
                width: 140
                button_color: "#2F42CD"
                text_color: "#FFFFFF"
                onReleased: {
                    root.reanalysis()
                }
            }
            CustomHorSpacerItem{}
        }

    }
}
